package com.djx.provider.service.impl;

import com.djx.comm.Pagination;
import com.djx.comm.util.ExcelUtils;
import com.djx.comm.util.UUIDUtil;
import com.djx.provider.dao.UserMapper;
import com.djx.comm.entity.User;
import com.djx.provider.service.UserService;
import com.djx.comm.util.StringUtils;
import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户接口实现
 * @author dingjunxiong
 */
@Service
public class UserServiceImpl implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    UserMapper userMapper;

    @Override
    public void add(User user) throws Exception {
        //数据加密
        user.aesEncryptVo();
        userMapper.add(user);
    }

    @Override
    public void update(User user) throws Exception {
        if (StringUtils.isNotBlank(user.getId())) {
            User vo = userMapper.queryById(user.getId());
            if (null != vo) {
                userMapper.update(user);
            }
        }
    }

    @Override
    public void delete(String id) {
        userMapper.delete(id);
    }

    @Override
    public User queryById(String id) throws Exception {
        User user = userMapper.queryById(id);
        if (null != user) {
            user.aesDecryptVo();
        }
        return user;
    }

    @Override
    public Pagination queryPage(User user) throws Exception {
        Pagination pagination = new Pagination();
        if (null != user.getPageNum() && user.getPageNum() > 0) {
            pagination.setPageNum(user.getPageNum());
        }
        if (null != user.getPageSize() && user.getPageSize() > 0) {
            pagination.setPageSize(user.getPageSize());
        }
        pagination.startPage();
        List<User> data = userMapper.queryList(user);
        pagination.setResult(data);
        if (null != data) {
            data.stream().forEach(x -> {
                try {
                    x.aesDecryptVo();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        return pagination;
    }

    @Override
    public void downloadFile(String fileName, String filePath, HttpServletResponse response) throws Exception {

    }

    @Override
    public void downloadExcel(User user, HttpServletResponse response) throws Exception {
        List<List<String>> dataList = new ArrayList<>();
        List<User> userList = userMapper.queryList(user);
        if (null != userList && !userList.isEmpty()) {
            userList.stream().forEach(x -> {
                List<String> data = new ArrayList<>();
                try {
                    x.aesDecryptVo();
                } catch (Exception e) {
                    logger.error("用户信息解密失败:" + x.toString());
                }
                data.add(x.getName());
                data.add(x.getAccount());
                data.add(x.getPassword());
                data.add(x.getPhone());
                data.add(x.getSex() == 1 ? "男" : "女");
                dataList.add(data);
            });
        }
        List<String> headList = Lists.newArrayList("姓名","账号","密码","手机号","性别");
        Workbook workbook = ExcelUtils.getWorkbookFromList(headList, dataList, false, "第一个页签");
        ExcelUtils.downLoadExcel("用户信息.xlsx", response, workbook);
    }

    @Override
    public void importExcel(MultipartFile file) {
        List<User> userList = new ArrayList<>();
        List<List<String>> dataList = ExcelUtils.readExcel(file, 0, 0);
        if (null != dataList && !dataList.isEmpty()) {
            dataList.stream().forEach(x -> {
                User user = User.builder()
                        .name(x.get(0))
                        .account(x.get(1))
                        .password(x.get(2))
                        .phone(x.get(3))
                        .sex(x.get(4).equals("男") ? 1 : 0)
                        .build();
                try {
                    user.setId(UUIDUtil.getUUID());
                    user.setStatus(0);
                    user.aesEncryptVo();
                    userList.add(user);
                } catch (Exception e) {
                    logger.error("import excel error:" + e.getMessage(), e);
                }
            });
            if (!userList.isEmpty()) {
                userMapper.addList(userList);
            }
        }
    }
}
